namespace JQuery.WebKit
{
    using System;
    using System.Collections;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;

    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public abstract class BaseItemCollection<OwnerType, ItemType> : StateManagedCollection//, IList<ItemType>, ICollection<ItemType>, IEnumerable<ItemType>
        where ItemType : class, IBaseItem, new()
    {
        private static readonly Type[] _knownTypes;
        private OwnerType _owner;
        private int capacity;

        static BaseItemCollection()
        {
            BaseItemCollection<OwnerType, ItemType>._knownTypes = new Type[] { typeof(ItemType) };
        }

        public BaseItemCollection()
        {
        }

        public BaseItemCollection(OwnerType owner)
        {
            this._owner = owner;
        }

        public int Add(ItemType item)
        {
            return ((IList)this).Add(item);
        }

        public bool Contains(ItemType item)
        {
            return ((IList)this).Contains(item);
        }

        public void CopyTo(ItemType[] array, int index)
        {
            this.CopyTo(array, index);
        }

        protected abstract override object CreateKnownType(int index);
        protected override Type[] GetKnownTypes()
        {
            return BaseItemCollection<OwnerType, ItemType>._knownTypes;
        }

        public int IndexOf(ItemType value)
        {
            return ((IList)this).IndexOf(value);
        }

        public void Insert(int index, ItemType item)
        {
            ((IList)this).Insert(index, item);
        }

        protected override void OnClear()
        {
            base.OnClear();
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            base.OnRemoveComplete(index, value);
        }

        protected override void OnValidate(object value)
        {
            base.OnValidate(value);
        }

        public void Remove(ItemType item)
        {
            this.Remove(item);
        }

        public void RemoveAt(int index)
        {
            this.RemoveAt(index);
        }

        protected override void SetDirtyObject(object o)
        {
            if (o is BaseItem)
            {
                ((BaseItem)o).SetDirty();
            }
        }

        public int Capacity
        {
            get
            {
                return this.capacity;
            }
            set
            {
                this.capacity = value;
            }
        }

        public ItemType this[int i]
        {
            get
            {
                return (ItemType)((IList)this)[i];
            }
            set
            {
                ((IList)this)[i] = value;
            }
        }

        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //    return this.GetEnumerator();
        //}

        //#region ICollection<ItemType> Members

        //void ICollection<ItemType>.Add(ItemType item)
        //{
        //    ((IList)this).Add(item);
        //    this.SetDirty();
        //}

        //public bool IsReadOnly
        //{
        //    get { return false; }
        //}

        //bool ICollection<ItemType>.Remove(ItemType item)
        //{
        //    if (((IList)this).Contains(item))
        //    {
        //        ((IList)this).Remove(item);
        //        return true;
        //    }
        //    return false;
        //}

        //#endregion

        //#region IEnumerable<ItemType> Members

        //public new IEnumerator<ItemType> GetEnumerator()
        //{
        //    return this.GetEnumerator();
        //}

        //#endregion
    }
}

